package com.wf.apicommons.config;
import cn.hutool.core.util.StrUtil;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: feign配置类
 */
public class FeignConfig implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 从header获取token，便于在feign调用的过程中解决token
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes srat = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = srat.getRequest();
        String token = request.getHeader("Authorization");
        if (StrUtil.isNotBlank(token)) {
            //将token传递出去
            requestTemplate.header("Authorization", token);
        }
    }


//    @Bean
//    Logger.Level feignLoggerLevel()
//    {
//        //除了HEADERS中定义的信息之外，还有请求和响应的正文及元数据。最详细的级别
//        return Logger.Level.FULL;
//        //默认的，不显示任何日志;
//        //return Logger.Level.NONE;
//        //仅记录请求方法、URL、响应状态码及执行时间;
//        //return Logger.Level.BASIC;
//        //除了BASIC中定义的信息之外，还有请求和响应的头信息;
//        //return Logger.Level.HEADERS;
//    }
}
